
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Test1 {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource() ;
        ((MysqlDataSource)dataSource).
                setUrl("jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("0118");

        //建立和数据库的连接
        Connection connection = dataSource.getConnection();
        //构造sql
         String sql = "insert into test values (?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
         int id = 3 ;
         String name = "李四";
        statement.setInt(1 , id);
        statement.setString(2, name);

        //发给服务器
        int n = statement.executeUpdate();
        System.out.println(n);

        //释放资源
        statement.close();
        connection.close();


    }
}
